# -*- coding: utf-8 -*-
# @Time : 2022/6/24 上午9:34
# @Author : wyf
# @FileName: login.py
# @Email: yunfei.wang@innvote.com
import execjs
import requests
from lxml import etree

headers = {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
    "Accept-Encoding": "gzip, deflate",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "Cache-Control": "no-cache",
    "Connection": "keep-alive",
    "Content-Type": "application/x-www-form-urlencoded",
    "Cookie": "PHPSESSID=b40nlk0d3papkck5hcgpfe4jq4; sidebarStatus=0; sg_logoutJob=think%3A%7B%22gwid%22%3A%22399%22%2C%22gwbh%22%3A%220107%22%2C%22gwmc%22%3A%22%25E7%25AE%25A1%25E7%2590%2586%25E5%2591%2598%22%2C%22gwbm%22%3A%22%25E7%25AE%25A1%25E7%2590%2586%25E5%2591%2598%22%2C%22mkids%22%3A%2203%252C0308%252C030801%252C030802%252C030803%252C030804%252C030805%252C030806%252C0309%252C030901%252C030902%252C0311%252C031101%252C031102%252C031106%252C0313%252C031301%252C031302%252C031303%252C031304%252C031305%252C031306%252C031307%252C031308%252C031309%252C031310%252C031311%252C0314%252C031401%252C031405%252C031408%252C031409%252C031410%252C031411%252C0315%252C031501%252C031502%252C0319%252C031901%252C031902%252C031903%252C0323%252C032301%252C032302%252C032303%252C032304%252C032305%252C032306%252C032307%252C0325%252C0328%252C032801%252C032802%252C032803%252C032804%252C032805%252C032806%252C032807%252C032808%252C032809%252C0332%252C033201%252C033202%252C033203%252C033204%252C033205%252C033206%252C033207%252C033208%252C033209%252C033210%252C0339%252C033901%252C033902%252C033903%252C033904%252C033905%252C033906%252C033907%252C033908%252C033909%252C0342%252C034201%252C0349%252C034901%252C034902%252C034903%252C0352%252C035201%252C035202%252C035203%252C0353%252C035301%252C035302%252C035303%252C0354%252C035401%252C035402%252C035403%252C035404%252C035405%252C0355%252C0356%252C035601%252C035602%252C035603%252C0357%252C035701%252C035702%252C035703%252C035704%252C035705%252C035706%252C0358%252C0359%252C0360%252C0361%252C036101%252C0362%252C0363%252C036301%252C0364%252C036401%252C0365%252C0366%252C036601%252C036602%252C036603%252C036604%252C0367%252C0368%252C0369%252C0370%252C037001%252C0371%252C037101%252C037102%252C037103%252C037104%252C0372%252C037201%252C037202%252C037203%252C037204%252C0373%252C0374%252C037402%252C037403%252C037404%252C0375%252C0376%252C0377%252C037701%252C037702%252C0378%252C037801%252C037802%252C037803%252C037804%252C0379%252C0380%252C0381%252C038101%252C0382%252C038201%252C038202%252C04%252C0402%252C040201%252C0404%252C040401%252C0407%252C040701%252C040702%252C040703%252C040704%252C040705%252C040706%252C040707%252C0408%252C040801%252C0409%252C040901%252C10%22%2C%22czids%22%3A%22136%252C142%252C85%252C86%252C87%252C92%252C143%252C144%252C145%252C76%252C75%252C88%252C89%252C90%252C109%252C110%252C131%252C93%252C83%252C84%252C137%252C94%252C120%252C111%252C112%252C113%252C114%252C115%252C116%252C117%252C118%252C119%252C121%252C122%252C123%252C124%252C125%252C126%252C127%252C128%252C129%252C130%252C150%252C24%252C25%252C26%252C27%252C73%252C134%252C28%252C29%252C30%252C31%252C74%252C135%252C95%252C96%252C97%252C98%252C99%252C141%252C100%252C101%252C102%252C103%252C104%252C105%252C106%252C107%252C108%252C139%252C140%252C146%252C147%252C148%252C149%252C78%252C79%252C80%252C81%252C82%22%2C%22appids%22%3A%22%22%7D",
    "Host": "192.168.1.222:84",
    "Origin": "http://192.168.1.222:84",
    "Pragma": "no-cache",
    "Referer": "http://192.168.1.222:84/Cert/Index/index.html",
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36"
}


url = "http://192.168.1.222:84/Cert/Index/index.html"
req = requests.get(url, headers=headers)
# print(req.text)

html = etree.HTML(req.text)
token = html.xpath("//*[@id='token']/@value")[0]
# print(t)

# var password = encrypt($("#password").val(), $("#token").val(), $("#iv").val());

js = """
CryptoJS = require('crypto-js')
function encrypt(str, key, iv) {
    var key = CryptoJS.enc.Hex.parse(key);
    var iv  = CryptoJS.enc.Hex.parse(iv);
    console.log(iv)
    var opinion = {iv:iv, padding:CryptoJS.pad.ZeroPadding};
    
    var encrypted = CryptoJS.AES.encrypt(str, key, opinion);
    encrypted = encrypted.toString();

    return encrypted;
}
"""
# token = "a858eed1e4c9be41e1af8a14cb8d00e2"
iv = "1234567890abcdef1234567890abcdef"
password = "123131"

context = execjs.compile(js).call("encrypt", password, token, iv)

print(context)


_body = {
    "account": "13062504716",
    "password": execjs.compile(js).call("encrypt", "504716", token, iv),
    "token": token,
    "iv": "1234567890abcdef1234567890abcdef",
    "yhxy": "on"
}


reqs = requests.post(url, data=_body, headers=headers)


print(reqs.text)
print(reqs.cookies)
